package main

import (
	"fmt"
	"log"
	"net/http"
)

// name: age
type Database map[string]int

func (d *Database) list(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "<html>\n<body>\n<ul>\n")
	for name, age := range *d {
		fmt.Fprintf(w, "<li>%s: %d</li>\n", name, age)
	}
	fmt.Fprintf(w, "</ul>\n</body>\n</html>\n")
}

func (d *Database) age(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get("name")
	fmt.Fprintf(w, "%s is %d years old\n", name, (*d)[name])
}

func main() {
	db := &Database{
		"Allen": 19,
		"Luffy": 18,
		"Zoro":  20,
		"Sanji": 19,
	}
	mux := http.NewServeMux()
	mux.Handle("/list", http.HandlerFunc(db.list))
	mux.HandleFunc("/age", db.age)
	err := http.ListenAndServe(":8001", mux)
	log.Fatal(err)

}
